package com.vim.modules.web.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vim.common.annotation.ParentModuleInfo;
import com.vim.common.model.response.PageRestfulResponse;
import com.vim.common.model.response.RestfulResponse;
import com.vim.common.utils.PageRestfulResponseUtils;
import com.vim.common.utils.ValidateUtils;
import com.vim.modules.web.model.ShopCart;
import com.vim.modules.web.model.ShopOrder;
import com.vim.modules.web.service.ShopCartService;
import com.vim.modules.web.service.ShopOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@ParentModuleInfo(moduleCode = "shop", moduleName = "电商平台")
@Api(value = "订单管理", description = "获取订单列表和订单详情")
@RestController
@RequestMapping(value = "/api/shop/order/")
public class ApiShopOrderController {

    @Resource
    private ShopOrderService shopOrderService;

    @Resource
    private ShopCartService shopCartService;

    @ApiOperation(value = "获取用户订单列表", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":[{\"orderId\":\"089ea528fd5d4cf193809e4c4ca6d16c\",\"orderSn\":\"001\",\"outerSn\":null,\"buyerId\":\"2796602e821144678e2084ce2f82a672\",\"buyerName\":\"448680\",\"buyerPlatform\":0,\"payMode\":null,\"payTime\":null,\"payMsg\":null,\"payStatus\":0,\"orderState\":10,\"orderStateStr\":\"待付款\",\"orderTotalPrice\":2399.00,\"orderTotalPriceStr\":\"2399.00\",\"orderPayPrice\":2399.00,\"orderPayPriceStr\":\"2399.00\",\"orderFreightPrice\":0.00,\"orderFreightPriceStr\":\"0.00\",\"orderPhone\":\"15528047640\",\"orderAddress\":\"湖北省武汉市江夏区金融港\",\"createTime\":1534072345000,\"orderGoodsList\":[{\"id\":\"41915e2aba684ea78638cd2c9e006d1c\",\"orderId\":\"089ea528fd5d4cf193809e4c4ca6d16c\",\"orderSn\":\"001\",\"goodsId\":\"3\",\"goodsName\":\"荣耀10  GT游戏加速 AIS手持夜景  6GB+64GB  幻影蓝全网通 移动联通电信4G 双卡双待\",\"goodsPrice\":2399.00,\"goodsPayPrice\":2399.00,\"goodsNum\":1,\"goodsImage\":\"http://m.360buyimg.com/mobilecms/s750x750_jfs/t18157/222/1822300674/231514/6c179af8/5ad87390N086a3c91.jpg!q80.jpg.webp\"}]},{\"orderId\":\"602e9d16a2784c718ea3c15a1d87268c\",\"orderSn\":\"001\",\"outerSn\":null,\"buyerId\":\"2796602e821144678e2084ce2f82a672\",\"buyerName\":\"448680\",\"buyerPlatform\":0,\"payMode\":null,\"payTime\":null,\"payMsg\":null,\"payStatus\":0,\"orderState\":10,\"orderStateStr\":\"待付款\",\"orderTotalPrice\":2399.00,\"orderTotalPriceStr\":\"2399.00\",\"orderPayPrice\":2399.00,\"orderPayPriceStr\":\"2399.00\",\"orderFreightPrice\":0.00,\"orderFreightPriceStr\":\"0.00\",\"orderPhone\":\"15528047640\",\"orderAddress\":\"湖北省武汉市江夏区金融港\",\"createTime\":1534072422000,\"orderGoodsList\":[{\"id\":\"93f455f215b040be87d5787b744c3f28\",\"orderId\":\"602e9d16a2784c718ea3c15a1d87268c\",\"orderSn\":\"001\",\"goodsId\":\"3\",\"goodsName\":\"荣耀10  GT游戏加速 AIS手持夜景  6GB+64GB  幻影蓝全网通 移动联通电信4G 双卡双待\",\"goodsPrice\":2399.00,\"goodsPayPrice\":2399.00,\"goodsNum\":1,\"goodsImage\":\"http://m.360buyimg.com/mobilecms/s750x750_jfs/t18157/222/1822300674/231514/6c179af8/5ad87390N086a3c91.jpg!q80.jpg.webp\"}]}],\"totalCount\":2,\"totalPage\":1,\"currentPage\":1,\"pageSize\":100}", httpMethod="GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true),
                        @ApiImplicitParam(name = "limit", value = "每页显示数量", required = true),
                        @ApiImplicitParam(name = "userId", value = "用户ID", required = true)})
    @RequestMapping(value = "/userOrderList")
    public PageRestfulResponse userOrderList(@RequestParam(value = "userId") String userId,
                                             @RequestParam(value = "page") Integer page,
                                             @RequestParam(value = "limit") Integer limit){
        ValidateUtils.checkNotBlank(userId);
        ValidateUtils.checkNotZero(page);
        ValidateUtils.checkNotZero(limit);
        PageHelper.startPage(page,limit);
        ShopOrder order = new ShopOrder();
        order.setBuyerId(userId);
        List<ShopOrder> list = shopOrderService.findList(order);
        PageInfo<ShopOrder> pageInfo = new PageInfo<>(list);
        return PageRestfulResponseUtils.handle(pageInfo);
    }

    @ApiOperation(value = "获取订单详情", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":{\"orderId\":\"602e9d16a2784c718ea3c15a1d87268c\",\"orderSn\":\"001\",\"outerSn\":null,\"buyerId\":\"2796602e821144678e2084ce2f82a672\",\"buyerName\":\"448680\",\"buyerPlatform\":0,\"payMode\":null,\"payTime\":null,\"payMsg\":null,\"payStatus\":0,\"orderState\":10,\"orderStateStr\":\"待付款\",\"orderTotalPrice\":2399.00,\"orderTotalPriceStr\":\"2399.00\",\"orderPayPrice\":2399.00,\"orderPayPriceStr\":\"2399.00\",\"orderFreightPrice\":0.00,\"orderFreightPriceStr\":\"0.00\",\"orderPhone\":\"15528047640\",\"orderAddress\":\"湖北省武汉市江夏区金融港\",\"createTime\":1534072422000,\"orderGoodsList\":null}}", httpMethod="GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderId", value = "订单ID", required = true)})
    @RequestMapping(value = "/orderDetail")
    public RestfulResponse orderDetail(@RequestParam(value = "orderId") String orderId){
        ValidateUtils.checkNotBlank(orderId);
        return new RestfulResponse(shopOrderService.find(orderId));
    }

    @ApiOperation(value = "加入购物车", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":{\"cartId\":\"5f63f2557f2f48a18c56f7c73d5f837b\",\"userId\":\"2796602e821144678e2084ce2f82a672\",\"goodsId\":\"1\",\"goodsName\":\"小辣椒 红辣椒R15 全面屏双摄拍照手机 6+128GB 黑色 全网通4G 双卡双待 大屏安卓手机京东自营\",\"goodsImage\":\"http://m.360buyimg.com/mobilecms/s750x750_jfs/t23131/54/1346507459/233648/178a183a/5b5af8d3N0fed8584.jpg!q80.jpg.webp\",\"goodsPrice\":1899.99,\"goodsPriceStr\":\"1899.99\",\"goodsNum\":1}}", httpMethod="POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true),
                        @ApiImplicitParam(name = "goodsId", value = "购物车ID列表", required = true),
                        @ApiImplicitParam(name = "goodsNum", value = "商品数量", required = true)})
    @RequestMapping(value = "/addCart", method = RequestMethod.POST)
    public RestfulResponse addCart(@RequestParam(value = "userId") String userId,
                                   @RequestParam(value = "goodsId") String goodsId,
                                   @RequestParam(value = "goodsNum") String goodsNum){
        ValidateUtils.checkNotBlank(userId);
        ValidateUtils.checkNotBlank(goodsId);
        ValidateUtils.checkNotBlank(goodsNum);
        ShopCart cart = shopOrderService.addCart(userId, goodsId, goodsNum);
        return new RestfulResponse(cart);
    }

    @ApiOperation(value = "删除购物车", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":\"OK\"}", httpMethod="POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "cartIds", value = "购物车ID列表", required = true)})
    @RequestMapping(value = "/delCart", method = RequestMethod.POST)
    public RestfulResponse delCart(@RequestParam(value = "cartIds") String cartIds){
        ValidateUtils.checkNotBlank(cartIds);
        shopOrderService.delCart(cartIds);
        return new RestfulResponse(RestfulResponse.OK);
    }

    @ApiOperation(value = "购物车列表", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":[{\"cartId\":\"0454fde3fd094d7ca8f607299ed32bbb\",\"userId\":\"2796602e821144678e2084ce2f82a672\",\"goodsId\":\"1\",\"goodsName\":\"小辣椒 红辣椒R15 全面屏双摄拍照手机 6+128GB 黑色 全网通4G 双卡双待 大屏安卓手机京东自营\",\"goodsImage\":\"http://m.360buyimg.com/mobilecms/s750x750_jfs/t23131/54/1346507459/233648/178a183a/5b5af8d3N0fed8584.jpg!q80.jpg.webp\",\"goodsPrice\":1899.99,\"goodsPriceStr\":\"1899.99\",\"goodsNum\":1}]}", httpMethod="GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true)})
    @RequestMapping(value = "/cartList")
    public RestfulResponse cartList(@RequestParam(value = "userId") String userId){
        ValidateUtils.checkNotBlank(userId);
        ShopCart cart = new ShopCart();
        cart.setUserId(userId);
        return new RestfulResponse(shopCartService.findList(cart));
    }

    @ApiOperation(value = "预下单", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":{\"address\":{\"addressId\":null,\"buyerId\":null,\"buyerName\":null,\"buyerPhone\":null,\"deliveryProvinceId\":null,\"deliveryProvince\":null,\"deliveryCityId\":null,\"deliveryCity\":null,\"deliveryAreaId\":null,\"deliveryArea\":null,\"deliveryStreet\":null,\"isDefault\":null,\"addressInfo\":null},\"cartList\":[{\"cartId\":\"0454fde3fd094d7ca8f607299ed32bbb\",\"userId\":\"2796602e821144678e2084ce2f82a672\",\"goodsId\":\"1\",\"goodsName\":\"小辣椒 红辣椒R15 全面屏双摄拍照手机 6+128GB 黑色 全网通4G 双卡双待 大屏安卓手机京东自营\",\"goodsImage\":\"http://m.360buyimg.com/mobilecms/s750x750_jfs/t23131/54/1346507459/233648/178a183a/5b5af8d3N0fed8584.jpg!q80.jpg.webp\",\"goodsPrice\":1899.99,\"goodsPriceStr\":\"1899.99\",\"goodsNum\":1}],\"goodsTotalPrice\":\"1899.99\",\"orderFreightPrice\":\"0.00\",\"orderTotalPrice\":\"1899.99\"}}", httpMethod="POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true),
                        @ApiImplicitParam(name = "cartIds", value = "购物车ID列表", required = true),
                        @ApiImplicitParam(name = "addressId", value = "地址ID", required = true)})
    @RequestMapping(value = "/preOrder", method = RequestMethod.POST)
    public RestfulResponse preOrder(@RequestParam(value = "userId") String userId,
                                    @RequestParam(value = "cartIds") String cartIds,
                                    @RequestParam(value = "addressId") String addressId){
        ValidateUtils.checkNotBlank(userId);
        ValidateUtils.checkNotBlank(cartIds);
        ValidateUtils.checkNotBlank(addressId);
        return new RestfulResponse(shopOrderService.preOrder(userId, cartIds, addressId));
    }

    @ApiOperation(value = "下单", notes = "{\"code\":20000,\"msg\":\"OK\",\"data\":\"OK\"}", httpMethod="POST")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = true),
            @ApiImplicitParam(name = "cartIds", value = "购物车ID列表", required = true),
            @ApiImplicitParam(name = "addressId", value = "地址ID", required = true)})
    @RequestMapping(value = "/createOrder", method = RequestMethod.POST)
    public RestfulResponse createOrder(@RequestParam(value = "userId") String userId,
                                       @RequestParam(value = "cartIds") String cartIds,
                                       @RequestParam(value = "addressId") String addressId){
        ValidateUtils.checkNotBlank(userId);
        ValidateUtils.checkNotBlank(cartIds);
        ValidateUtils.checkNotBlank(addressId);
        return new RestfulResponse(shopOrderService.createOrder(userId, cartIds, addressId));
    }
}
